package com.body;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.connect.connect;

public class takemessage extends HttpServlet{
	
	private String usernumber;
	private String tousernumber;
	private String username;
	private String tousername;
	private String content ;            
	private Date date;
	private connect conn;         
	private Connection con;         
	private PrintWriter out;      
	private String times;           
	private String mutex = "";           
	private Statement stmt;
	private HttpSession session;  // session
	private ResultSet rs;
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		synchronized (mutex){
			 
			tousernumber = req.getParameter("number");
			content = new String(req.getParameter("content").getBytes("ISO8859_1"),"UTF-8");
			date = new Date();
			SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd");
			times=timeFormat.format(date);
	
			session = req.getSession(); 
			usernumber = (String)session.getAttribute("username");
			
			session.setAttribute("tousernumber",tousernumber);
			session.setAttribute("content", content);
			
			conn = new connect();
			con = conn.con();
			out = resp.getWriter();
			resp.setHeader("Content-type", "text/html;charset=utf-8");
			resp.setCharacterEncoding("utf-8");
			resp.setCharacterEncoding("utf-8");
			
			String StrYes = (String )session.getAttribute("yes");
			
			if(!("yes".equals(StrYes))){

				
				out.print("<script>alert('Please Login First!');</script>");
				out.print("<script  type='text/javascript'  >; window.location.href='login.jsp?from=2';  </script> ");
				return ;
			} else {
				try{
					stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
					rs = stmt.executeQuery("SELECT * FROM person WHERE UserNumber='"+usernumber+"'");
					if(rs.next()){
						username=rs.getString("UserName");
					}
					
					rs = stmt.executeQuery("SELECT * FROM person WHERE UserNumber='"+tousernumber+"'");
					if(rs.next()){
						tousername = rs.getString("UserName");
						stmt.executeUpdate("INSERT INTO message(UserNumber,ToUserNumber,UserName,ToUserName,Content,Time)"
					      		+ "values('"+usernumber+"','"+tousernumber+"','"+username+"','"+tousername+"','"+content+"','"+times+"')");
						session.setAttribute("tousernumber","");
						session.setAttribute("content", "");
						session.setAttribute("messagetime",times);
						session.setAttribute("messagecontent", content);
						session.setAttribute("messageusername", tousername);
						
						out.print("<script>alert('Successfully taking message!');</script>");
						out.print("<script  type='text/javascript'>; window.location.href='message_look.jsp';</script> ");
					} else {
						out.print("<script>alert('False to find person!');history.go(-1);</script>");
					}
					con.close();					
				}catch(SQLException e){
				    e.printStackTrace();
			    }
				return ;
			}
		}
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

}
